package com.rs.qm.modules.member.controller;

import com.rs.qm.common.utils.PageUtils;
import com.rs.qm.common.utils.R;
import com.rs.qm.common.utils.RandomUtil;
import com.rs.qm.common.utils.StringUtil;
import com.rs.qm.modules.member.entity.EnterpriseNameLibEntity;
import com.rs.qm.modules.member.service.EnterpriseNameLibService;
import com.rs.qm.modules.sys.entity.SysAreaEntity;
import com.rs.qm.modules.sys.entity.SysBusinessCategoryEntity;
import com.rs.qm.modules.sys.service.SysAreaService;
import com.rs.qm.modules.sys.service.SysBusinessCategoryService;
import jdk.nashorn.internal.objects.annotations.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.*;


/**
 * 企业名称库,id 10000-19999:单字 ,20000-2999:双字,30000-39999:三字,40000-49999:四字
 *
 * @author GuoXunMin
 * @email v450625169@126.com
 * @date 2020-01-02 10:53:35
 */
@Slf4j
@Configuration
@RestController
@RequestMapping("pc/enterprisenamelib")
public class EnterpriseNameLibController {

    @Value("${static-data.max-idx}")
    private Integer[] maxIdxs;

    @Autowired
    private EnterpriseNameLibService enterpriseNameLibService;
    @Autowired
    private SysAreaService sysAreaService;
    @Autowired
    private SysBusinessCategoryService sysBusinessCategoryService;
    /**
     * 根据城市信息和行业信息生成企业名称
     *      一次10条
     *      随机取两个字(单字拼,就双字),三个字,四个字
     *      单字id:10000-19999,2字:20000-29999,3字:30000-39999,4字:40000-49999
     *      根据名称hash值取模 判断 通过率的 高中低
     * @param cityName
     * @param category
     * @return
     */
    @GetMapping("/names")
    public R getNames(String cityName,String category){
        if(StringUtil.isEmpty(cityName)){
            return R.error(10000,"城市名:cityName 不可为空!");
        }
        if(StringUtil.isEmpty(category)){
            return R.error(10001,"行业名:category 不可为空!");
        }
        Map<Long,EnterpriseNameLibEntity> allNames = enterpriseNameLibService.queryAllNames();
        Set<Map<String,String>> resultSet = new HashSet<>();
        while (resultSet.size() < 10){
            Map<String,String> map = new HashMap<>();
            String name = createOneNameByLib(allNames);
            if(!name.isEmpty()){
                map.put("name",cityName+name+category+"有限公司");
                map.put("tgl",getTgl(cityName+name+category+"有限公司"));
                resultSet.add(map);
            }
        }
        return R.okData(resultSet);
    }

    @GetMapping("/verify/name")
    public R verifyName(String cityName,String name,String category){
        return R.okData(getTgl(cityName+name+category));
    }

    /**
     * 生成一个名称,并算出通过率(高中低),放入 set集合中
     * @param allNames
     * @return String
     */
    private String createOneNameByLib(Map<Long, EnterpriseNameLibEntity> allNames) {
        Integer oneMaxIdx = maxIdxs[0];
        Integer twoMaxIdx = maxIdxs[1];
        Integer threeMaxIdx = maxIdxs[2];
        String name = null;
        //用随机数 取下 要生成 几个字的名称
        int randomIntZs = RandomUtil.getInt(1,3);
        if(randomIntZs == 1){
            name = allNames.get(Long.valueOf(RandomUtil.getInt(10000, 10000 + oneMaxIdx))).getName() + allNames.get(Long.valueOf(RandomUtil.getInt(10000, 10000 + oneMaxIdx))).getName();
        }
        if(randomIntZs == 2){
            name = allNames.get(Long.valueOf(RandomUtil.getInt(20000, 20000 + twoMaxIdx))).getName();
        }
        if(randomIntZs == 3){
            name = allNames.get(Long.valueOf(RandomUtil.getInt(30000, 30000 + threeMaxIdx))).getName();
        }
        return name;
    }

    /**
     * 名字通过率伪算法
     * 字符串 hashcode值 取模
     * @param str
     * @return
     */
    private String getTgl(String str){
        int resultQuMo = Math.abs(str.hashCode() % 3);
        String result = null;
        switch(resultQuMo){
            case 0 :
                result = "低";
                break; //可选
            case 1 :
                result = "中";
                break; //可选
            case 2 :
                result = "高";
                break; //可选
            default : //可选
                log.error("getTgl,出现了不该出的值");
                result = "高";
        }

        return result;
    }
   /* *//**
     *
     * 列表
     *//*
    @GetMapping("/list")
    @RequiresPermissions("generator:enterprisenamelib:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = enterpriseNameLibService.queryPage(params);

        return R.ok().put("page", page);
    }


    *//**
     * 信息
     *//*
    @GetMapping("/info/{nameId}")
    @RequiresPermissions("generator:enterprisenamelib:info")
    public R info(@PathVariable("nameId") Long nameId){
		EnterpriseNameLibEntity enterpriseNameLib = enterpriseNameLibService.selectById(nameId);

        return R.ok().put("enterpriseNameLib", enterpriseNameLib);
    }

    *//**
     * 保存
     *//*
    @PostMapping("/save")
    @RequiresPermissions("generator:enterprisenamelib:save")
    public R save(@RequestBody EnterpriseNameLibEntity enterpriseNameLib){
		enterpriseNameLibService.insert(enterpriseNameLib);

        return R.ok();
    }

    *//**
     * 修改
     *//*
    @PutMapping("/update")
    @RequiresPermissions("generator:enterprisenamelib:update")
    public R update(@RequestBody EnterpriseNameLibEntity enterpriseNameLib){
		enterpriseNameLibService.updateById(enterpriseNameLib);

        return R.ok();
    }

    *//**
     * 删除
     *//*
    @DeleteMapping("/delete")
    @RequiresPermissions("generator:enterprisenamelib:delete")
    public R delete(@RequestBody Long[] nameIds){
		enterpriseNameLibService.deleteBatchIds(Arrays.asList(nameIds));

        return R.ok();
    }*/

}
